﻿using System;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Collections;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

using com.fly.sms;

namespace com.fly.sms
{
    public class SendPoolTaskAction:BaseAction
    {
        public SendPoolTaskAction(HttpContext contextTemp)
        {
            this.context = contextTemp;
        }
        public override void excute()
        {
            string Name=context.Request["Name"];
            string MsgContent=context.Request["MsgContent"];
            string CustomerNumberPoolId = context.Request["CustomerNumberPoolId"];

            string FromDate = context.Request["FromDate"];
            string ToDate = context.Request["ToDate"];

            string TaskType = context.Request["TaskType"];

            string Condition = context.Request["Condition"];

            string StartTime = context.Request["StartTime"];
            

            int msgLength=MsgContent.Length;
            int msgCount = 1;
            if (msgLength>70)
            {
                if (msgLength % 65 == 0)
                {
                    msgCount = msgLength / 65;
                }
                else {
                    msgCount = msgLength / 65 + 1;
                }
            }

            string[] DestNumberArray = CustomerNumberPoolDetailService.findByNumberPoolId(CustomerNumberPoolId);

            //添加待发送短信
            bool rst = SMTaskService.add(guid, guid,CustomerId, Name, MsgContent, msgCount * DestNumberArray.Length, DestNumberArray.Length, TaskType, FromDate, ToDate, Condition, StartTime);
            //或缺添加后的Id
           // string SMTaskId = SMTaskService.getId(Name);
            foreach(string mobile in DestNumberArray){
                SMTaskDetailService.add(guid, mobile, msgCount);
            }
            ht.Add("responseCode", "200");
            ht.Add("responseData", rst);
            string json = JsonConvert.SerializeObject(ht);
            context.Response.Write(json);           
        }
    }
}